package com.ruoyi.edh.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.vo.EdhFinanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edh.mapper.EdhFinanceMapper;
import com.ruoyi.edh.domain.EdhFinance;
import com.ruoyi.edh.service.IEdhFinanceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 易订货-财务结算Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-19
 */
@Service
public class EdhFinanceServiceImpl implements IEdhFinanceService 
{
    @Autowired
    private EdhFinanceMapper edhFinanceMapper;

    /**
     * 查询易订货-财务结算
     * 
     * @param id 易订货-财务结算ID
     * @return 易订货-财务结算
     */
    @Override
    public EdhFinance selectEdhFinanceById(Long id)
    {
        return edhFinanceMapper.selectEdhFinanceById(id);
    }

    /**
     * 查询易订货-财务结算列表
     * 
     * @param edhFinance 易订货-财务结算
     * @return 易订货-财务结算
     */
    @Override
    public List<EdhFinanceVo> selectEdhFinanceList(EdhFinanceVo edhFinanceVo)
    {
        return edhFinanceMapper.querySupplierIncome(edhFinanceVo);
    }

    /**
     * 新增易订货-财务结算
     * 
     * @param edhFinance 易订货-财务结算
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEdhFinance(EdhFinance edhFinance)
    {
        edhFinance.setCreateTime(DateUtils.getNowDate());
        EdhFinanceVo edhFinanceVo=new EdhFinanceVo();
        edhFinanceVo.setId(edhFinance.getSupplierId());
        List<EdhFinanceVo> edhFinanceVos = edhFinanceMapper.querySupplierIncome(edhFinanceVo);
        EdhFinanceVo edhFinanceVo1 = edhFinanceVos.get(0);
        //获得订单总钱数
        BigDecimal orderAmount = edhFinanceVo1.getOrderAmount();
        //获得上一次结算的钱
        BigDecimal settledAmount = edhFinanceVo1.getSettledAmount();
        //本次结算的钱
        BigDecimal amount = orderAmount.subtract(settledAmount);
        //得到这次结算后的钱
        BigDecimal settledAmountAdd = settledAmount.add(amount);

        edhFinance.setCreateBy(SecurityUtils.getUserId().toString());
        edhFinance.setCreateTime(new Date());

        edhFinance.setSettlementAmount(amount);
        edhFinance.setSettledAmount(settledAmountAdd);
        return edhFinanceMapper.insertEdhFinance(edhFinance);
    }

    /**
     * 修改易订货-财务结算
     * 
     * @param edhFinance 易订货-财务结算
     * @return 结果
     */
    @Override
    public int updateEdhFinance(EdhFinance edhFinance)
    {
        edhFinance.setUpdateTime(DateUtils.getNowDate());
        return edhFinanceMapper.updateEdhFinance(edhFinance);
    }

    /**
     * 批量删除易订货-财务结算
     * 
     * @param ids 需要删除的易订货-财务结算ID
     * @return 结果
     */
    @Override
    public int deleteEdhFinanceByIds(Long[] ids)
    {
        return edhFinanceMapper.deleteEdhFinanceByIds(ids);
    }

    /**
     * 删除易订货-财务结算信息
     * 
     * @param id 易订货-财务结算ID
     * @return 结果
     */
    @Override
    public int deleteEdhFinanceById(Long id)
    {
        return edhFinanceMapper.deleteEdhFinanceById(id);
    }
}
